package com.sba.core.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;

import com.sba.core.domain.Organization;
import com.sba.core.domain.Region;
import com.sba.core.repository.OrganizationRepository;
import com.sba.core.repository.RegionRepository;
import com.sba.core.service.OrganizationService;

/**
 * 
 * @author feiliming
 *
 */
@Service
public class OrganizationServiceImpl extends BaseServiceImpl<Organization> implements OrganizationService{
	
	@Autowired
	OrganizationRepository organizationRepository;
	
	@Autowired
	RegionRepository regionRepository;
	
	@Override
	public Organization getById(Long id){
		Organization organization = organizationRepository.findOne(id);
		if(organization.getParentId() != null && organization.getParentId() != 0){
			Organization porganization = organizationRepository.findOne(organization.getParentId());
			organization.setParentName(porganization.getName());
		}else{
			organization.setParentName("");
		}
		if(organization.getRegionId() != null && organization.getRegionId() != 0){
			Region region = regionRepository.findOne(organization.getRegionId());
			organization.setRegionName(region.getName());
		}else{
			organization.setRegionName("");
		}
		return organization;
	}
	
	@Override
	public Page<Organization> getOrganizations(int limit, int offset) {
		return organizationRepository.findByIsDelete(0, buildPageRequest(limit, offset));
	}
	
	@Override
	public Page<Organization> getOrganizationsByNameLike(String name, int limit, int offset) {
		return organizationRepository.findByNameLikeAndIsDelete("%" + name + "%", 0, buildPageRequest(limit, offset));
	}

	@Override
	public Page<Organization> getOrganizationsByParentId(Long parentId, int limit, int offset) {
		return organizationRepository.findByParentIdAndIsDelete(parentId, 0, buildPageRequest(limit, offset));
	}
	
	@Override
	public Page<Organization> getOrganizationsByparentIdAndNameLike(Long parentId, String name, int limit, int offset) {
		return organizationRepository.findByParentIdAndNameLikeAndIsDelete(parentId, "%" + name + "%", 0, buildPageRequest(limit, offset));
	}
	
	@Override
	public Page<Organization> getOrganizationsByOrganizationType(String regionType, int limit, int offset) {
		return organizationRepository.findByOrganizationTypeAndIsDelete(regionType, 0, buildPageRequest(limit, offset));
	}
	
	@Override
	public Page<Organization> getOrganizationsByOrganizationTypeAndNameLike(String regionType, String name, int limit, int offset) {
		return organizationRepository.findByOrganizationTypeAndNameLikeAndIsDelete(regionType, "%" + name + "%", 0, buildPageRequest(limit, offset));
	}
	
	private PageRequest buildPageRequest(int limit, int offset){
		Order typeOrder = new Order(Direction.ASC, "organizationType");
		Order sortOrder = new Order(Direction.ASC, "sort");
		Order nameOrder = new Order(Direction.ASC, "name");
		Sort sort = new Sort(typeOrder, sortOrder, nameOrder);
		int pageNumber = (offset + limit)/limit;
		int pageSize = limit;
		PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, sort);
		return pageRequest;
	}

}
